package com.unbox.common.annotation.validation;
import com.unbox.common.annotation.NotNumber;
import com.unbox.common.annotation.entity.AnnotionEnitty;
import com.unbox.common.util.UnBoxUtils;
import com.unbox.common.util.UnboxLog;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class ValidNotNumber implements ValidationCompare {

    // 国际化日志输出
    UnboxLog log = new UnBoxUtils.LocaleMessageLog(ValidNotNull.class);

    @Override
    public boolean compare(Annotation annotation, AnnotionEnitty annotionEnitty) throws Exception {
        NotNumber notNumber = (NotNumber) annotation;
        int index = notNumber.index();
        Object obj = annotionEnitty.getValues()[index];
        Class cls = obj.getClass();
        try {
            Field field = cls.getDeclaredField(notNumber.name());
            field.setAccessible(true);
            Object value = field.get(obj);
            field.setAccessible(false);
            Long.valueOf(value.toString());
        } catch (NoSuchFieldException e) {
            log.error("validation.info.aop.error.msg", e.getMessage());
            throw new Exception(notNumber.msg());
        } catch (IllegalAccessException e) {
            log.error("validation.info.aop.error.msg", e.getMessage());
            throw new Exception(notNumber.msg());
        } catch (Exception e) {
            log.error("validation.info.aop.error.msg", e.getMessage());
            throw new Exception(notNumber.msg());
        }
        return true;
    }
}
